#!/bin/bash
#
# Copyright (C) 2013-2021 Spring Studio
# Copyright (C) 2013-2021 Dou Dongchun <doudongchun@163.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

########### library variables ##########
URI=
GIT=https://gitee.com/springstudio/PROJ.git
COMMIT=8.2.1
LOCAL=proj
PLATFORM=

########### build library ##########

build_arch()
{
  if [[ $PLATFORM == "cmake" ]]; then
    configure_cmake
  else
    configure_autoconf
  fi

  make clean
  make -j4 -s
  make install
}

configure_autoconf()
{
  ./autogen.sh

  rm -rf build
  mkdir build
  cd build

  # export PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig
  export SQLITE3_LIBS=$PREFIX/lib/libsqlite3.so
  export SQLITE3_CFLAGS=-I$PREFIX/include
  ../configure \
    --prefix=$PREFIX \
    --host=$TARGET \
    --with-sysroot=$SYSROOT \
    --disable-tiff \
    --without-curl
}

configure_cmake()
{
  rm -rf build
  mkdir build
  cd build

  cmake \
    -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
    -DCMAKE_INSTALL_PREFIX=$PREFIX \
    -DCMAKE_BUILD_TYPE=Release \
    -DANDROID_PLATFORM=$API \
    -DANDROID_ABI=$ABI \
    -DANDROID_NDK=$ANDROID_NDK \
    -DANDROID_TOOLCHAIN=clang \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DBUILD_APPS=OFF \
    -DEXE_SQLITE3=/usr/bin/sqlite3 \
    -DSQLITE3_INCLUDE_DIR=$PREFIX/include \
    -DSQLITE3_LIBRARY=$PREFIX/lib/libsqlite3.so \
    -DENABLE_CURL=OFF \
    -DCURL_INCLUDE_DIR=$PREFIX/include \
    -DCURL_LIBRARY=$PREFIX/lib/libcurl.so \
    -DENABLE_TIFF=OFF \
    -DTIFF_INCLUDE_DIR=$PREFIX/include \
    -DTIFF_LIBRARY_RELEASE=$PREFIX/lib/libtiff.so \
    ..
}

clear_arch() {
  if [ -d build ]; then
    cd build && make clean && cd -
    rm -rf build
  fi
}

PS3='Enter your compile platform: '
options=("autoconf" "cmake")
select opt in "${options[@]}"
do
    case $opt in
        autoconf|cmake)
            PLATFORM=$opt
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

############# forbid modify ###########

. ./tools/do-compile.sh
